home *** CD-ROM | disk | FTP | other *** search
/ PC Home 138 / PC Home issue 138.iso / Software / Essentials / Netscape / browser.xpi / bin / defaults / autoconfig / prefcalls.js
Encoding:
Text File  |  2002-06-19  |  6.8 KB  |  216 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  * Mitesh Shah <mitesh@netscape.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the NPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the NPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. const nsILDAPURL = Components.interfaces.nsILDAPURL;
  40. const LDAPURLContractID = "@mozilla.org/network/ldap-url;1";
  41. const nsILDAPSyncQuery = Components.interfaces.nsILDAPSyncQuery;
  42. const LDAPSyncQueryContractID = "@mozilla.org/ldapsyncquery;1";
  43. const nsIPrefService = Components.interfaces.nsIPrefService;
  44. const PrefServiceContractID = "@mozilla.org/preferences-service;1";
  45.  
  46. function getPrefBranch() {
  47.     
  48.     var prefService = Components.classes[PrefServiceContractID]
  49.                                 .getService(nsIPrefService);    
  50.     return prefService.getBranch(null);
  51. }
  52.  
  53. function pref(prefName, value) {
  54.  
  55.     try { 
  56.         var prefBranch = getPrefBranch();
  57.  
  58.         if (typeof value == "string") {
  59.             prefBranch.setCharPref(prefName, value);
  60.         }
  61.         else if (typeof value == "number") {
  62.             prefBranch.setIntPref(prefName, value);
  63.         }
  64.         else if (typeof value == "boolean") {
  65.             prefBranch.setBoolPref(prefName, value);
  66.         }
  67.     }
  68.     catch(e) {
  69.         displayError("pref", e);
  70.     }
  71. }
  72.  
  73. function defaultPref(prefName, value) {
  74.     
  75.     try {
  76.         var prefService = Components.classes[PrefServiceContractID]
  77.                                     .getService(nsIPrefService);        
  78.         var prefBranch = prefService.getDefaultBranch(null);
  79.         if (typeof value == "string") {
  80.             prefBranch.setCharPref(prefName, value);
  81.         }
  82.         else if (typeof value == "number") {
  83.             prefBranch.setIntPref(prefName, value);
  84.         }
  85.         else if (typeof value == "boolean") {
  86.             prefBranch.setBoolPref(prefName, value);
  87.         }
  88.     }
  89.     catch(e) {
  90.         displayError("defaultPref", e);
  91.     }
  92. }
  93.  
  94. function lockPref(prefName, value) {
  95.     
  96.     try {
  97.         var prefBranch = getPrefBranch();
  98.         
  99.         if (prefBranch.prefIsLocked(prefName))
  100.             prefBranch.unlockPref(prefName);
  101.         
  102.         defaultPref(prefName, value);
  103.         
  104.         prefBranch.lockPref(prefName);
  105.     }
  106.     catch(e) {
  107.         displayError("lockPref", e);
  108.     }
  109. }
  110.  
  111. function unlockPref(prefName) {
  112.  
  113.     try {
  114.  
  115.         var prefBranch = getPrefBranch();
  116.         prefBranch.unlockPref(prefName);
  117.     }
  118.     catch(e) {
  119.         displayError("unlockPref", e);
  120.     }
  121. }
  122.  
  123. function getPref(prefName) {
  124.     
  125.     try {
  126.         var prefBranch = getPrefBranch();
  127.         
  128.         switch (prefBranch.getPrefType(prefName)) {
  129.             
  130.         case prefBranch.PREF_STRING:
  131.             return prefBranch.getCharPref(prefName);
  132.             
  133.         case prefBranch.PREF_INT:
  134.             return prefBranch.getIntPref(prefName);
  135.             
  136.         case prefBranch.PREF_BOOL:
  137.             return prefBranch.getBoolPref(prefName);
  138.         default:
  139.             return null;
  140.         }
  141.     }
  142.     catch(e) {
  143.         displayError("getPref", e);
  144.     }
  145. }
  146.  
  147.  
  148. function getLDAPAttributes(host, base, filter, attribs) {
  149.     
  150.     try {
  151.         var url = Components.classes[LDAPURLContractID].createInstance(nsILDAPURL);
  152.     
  153.         url.spec = "ldap://" + host + "/" + base + "?" + attribs 
  154.                    + "?sub?" +  filter;
  155.         var ldapquery = Components.classes[LDAPSyncQueryContractID]
  156.                                   .createInstance(nsILDAPSyncQuery);
  157.         processLDAPValues(ldapquery.getQueryResults(url));     // user supplied method
  158.     }
  159.     catch(e) {
  160.         displayError("getLDAPAttibutes", e);
  161.     }
  162. }
  163.  
  164. function getLDAPValue(str, key) {
  165.  
  166.     try {
  167.         if (str == null || key == null)
  168.             return null;
  169.         
  170.         var search_key = "\n" + key + "=";
  171.         
  172.         var start_pos = str.indexOf(search_key);
  173.         if (start_pos == -1)
  174.             return null;
  175.         
  176.         start_pos += search_key.length;
  177.         
  178.         var end_pos = str.indexOf("\n", start_pos);
  179.         if (end_pos == -1)
  180.             end_pos = str.length;
  181.         
  182.         return str.substring(start_pos, end_pos);
  183.     }
  184.     catch(e) {
  185.         displayError("getLDAPValue", e);
  186.     }
  187. }
  188.  
  189. function displayError(funcname, message) {
  190.  
  191.     try {
  192.         var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  193.                                       .getService(Components.interfaces.nsIPromptService);
  194.         var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
  195.                                .getService(Components.interfaces.nsIStringBundleService)
  196.                                .createBundle("chrome://autoconfig/locale/autoconfig.properties");
  197.  
  198.          var title = bundle.GetStringFromName("autoConfigTitle");
  199.          var msg = bundle.formatStringFromName("autoConfigMsg", [funcname], 1);
  200.          promptService.alert(null, title, msg + " " + message);
  201.     }
  202.     catch(e) { }
  203. }
  204.  
  205. function getenv(name) {
  206.     
  207.     try {
  208.         var currentProcess=Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
  209.         return currentProcess.getEnvironment(name);
  210.     }
  211.     catch(e) {
  212.         displayError("getEnvironment", e);
  213.     }
  214. }
  215.  
  216.